package moodish.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Panel;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import moodish.comm.ClientMessage;
import moodish.dummy.ClientCommDummy;

public class Window_Main extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextField txtName;
	private JButton btnOptions = new JButton("Options");
	private JButton btnSignOut = new JButton("Sign Out");
	private JScrollPane scrollPaneFriends = new JScrollPane();
	private JScrollPane scrollPaneOtherPeople = new JScrollPane();
	private JRadioButton rdbtnOnline = new JRadioButton("Online");
	JTextArea txtrMood;

	private ClientCommDummy clientComm;

	private Vector<Friend> friends = new Vector<Friend>();
	private Vector<OtherPeople> otherPeople = new Vector<OtherPeople>();

	private DefaultListModel<Friend> modelFriend = new DefaultListModel<Friend>();
	private JList<Friend> friendsList = new JList<Friend>(modelFriend);

	private DefaultListModel<OtherPeople> modelOther = new DefaultListModel<OtherPeople>();
	private JList<OtherPeople> otherPeopleList = new JList<OtherPeople>(
			modelOther);

	/**
	 * Create the frame.
	 * 
	 * @param clientComm
	 * @param username
	 */
	public Window_Main(ClientCommDummy clientComm, String username) {
		this.setTitle("Moodish");
		this.clientComm = clientComm;
		buildGui(username);
		setVisible(true);
		new Thread() {
			public void run() {
				displayMsg();
			}
		}.start();
	}

	private synchronized void displayMsg() {
		Iterator<Friend> i;
		Iterator<OtherPeople> io;
		Friend f;
		OtherPeople other;
		while (clientComm.isConnected()) {
			i = friends.iterator();
			io = otherPeople.iterator();
			ClientMessage next = clientComm.getNextMessage();

			switch (next.getType()) {
			case CONNECTED:
				other = new OtherPeople(next.getPayload());
				otherPeople.add(other);
				modelOther.addElement(other);
				break;

			case DISCONNECTED:
				while (i.hasNext()) {
					f = i.next();
					if (f.getName().equals(next.getPayload())) {
						friendsList.remove(modelFriend.indexOf(f));
						modelFriend.removeElement(f);
						i.remove();
					}
				}
				while (io.hasNext()) {
					OtherPeople o = io.next();
					if (o.getName().equals(next.getPayload())) {
						otherPeopleList.remove(modelOther.indexOf(o));
						modelOther.removeElement(o);
						io.remove();
					}
				}
				break;
			case MOODISH_MESSAGE:
				for (int j = 0; j < friends.size(); j++) {
					f = friends.get(j);
					if (f.getName().equals(next.getSendersNickname())) {
						f.setMood(next.getPayload());
						break;
					}
				}
				break;
			case ERROR:
				JOptionPane.showMessageDialog(null, "An error as occurred");
				break;
			default:
				break;

			}
			scrollPaneFriends.repaint();
			scrollPaneOtherPeople.repaint();
		}
	}

	private class Listener implements ActionListener {
		private Window_Main frame;

		private void setWB(Window_Main frame) {
			this.frame = frame;
		}

		@Override
		public void actionPerformed(ActionEvent e) {

			if (e.getSource().equals(btnOptions)) {
				Window_Option optionFrame = new Window_Option(clientComm,
						getWindow_Main());
				optionFrame.setVisible(true);

			}
			if (e.getSource().equals(rdbtnOnline)) {
				rdbtnOnline.setSelected(true);
			}
			if (e.getSource().equals(btnSignOut)) {
				clientComm.disconnect();
				frame.dispose();
				System.exit(0);
			}
		}
	}

	private void buildGui(String username) {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 805, 750);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		Listener listener = new Listener();
		listener.setWB(this);

		Panel MyArea = new Panel();
		MyArea.setBounds(15, 588, 768, 124);
		contentPane.add(MyArea);
		MyArea.setBackground(new Color(100, 149, 237));
		MyArea.setLayout(null);

		txtName = new JTextField();
		txtName.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
		txtName.setBackground(new Color(100, 149, 237));
		txtName.setForeground(SystemColor.window);
		txtName.setText(username);
		txtName.setEditable(false);
		txtName.setBounds(15, 24, 227, 30);
		MyArea.add(txtName);
		txtName.setColumns(10);

		txtrMood = new JTextArea();
		txtrMood.setFont(new Font("Monospaced", Font.PLAIN, 16));
		txtrMood.setText("Mood");
		txtrMood.setBounds(15, 89, 122, 26);
		MyArea.add(txtrMood);
		txtrMood.setEditable(false);
		btnOptions.setBackground(new Color(230, 230, 250));

		btnOptions.setBounds(636, 42, 122, 29);
		MyArea.add(btnOptions);
		btnOptions.addActionListener(listener);

		rdbtnOnline.setForeground(Color.WHITE);
		rdbtnOnline.setBackground(new Color(100, 149, 237));
		rdbtnOnline.setBounds(148, 88, 94, 29);
		MyArea.add(rdbtnOnline);
		rdbtnOnline.addActionListener(listener);
		rdbtnOnline.setSelected(true);
		rdbtnOnline.setFont(new Font("Monospaced", Font.PLAIN, 16));
		btnSignOut.setBackground(new Color(230, 230, 250));

		btnSignOut.setBounds(636, 88, 122, 29);
		MyArea.add(btnSignOut);

		JLabel lblMood = new JLabel("MooD");
		lblMood.setForeground(Color.WHITE);
		lblMood.setFont(new Font("Tekton Pro", Font.PLAIN, 20));
		lblMood.setBounds(15, 66, 69, 20);
		MyArea.add(lblMood);

		JPanel OthersPanel = new JPanel();
		OthersPanel.setBackground(Color.WHITE);
		OthersPanel.setBounds(15, 16, 768, 570);
		contentPane.add(OthersPanel);
		OthersPanel.setLayout(null);

		scrollPaneFriends.setBounds(0, 52, 380, 518);
		OthersPanel.add(scrollPaneFriends);

		scrollPaneOtherPeople.setBounds(382, 52, 386, 518);
		OthersPanel.add(scrollPaneOtherPeople);

		JLabel lblHowYourFriends = new JLabel("How your Friends are feeling:");
		lblHowYourFriends.setFont(new Font("Tahoma", Font.PLAIN, 19));
		lblHowYourFriends.setBounds(15, 16, 365, 20);
		OthersPanel.add(lblHowYourFriends);

		JLabel lblOtherPeopleOnline = new JLabel("Other People Online");
		lblOtherPeopleOnline.setFont(new Font("Tahoma", Font.PLAIN, 19));
		lblOtherPeopleOnline.setBounds(395, 16, 373, 20);
		OthersPanel.add(lblOtherPeopleOnline);
		btnSignOut.addActionListener(listener);

		scrollPaneOtherPeople.setViewportView(otherPeopleList);
		scrollPaneFriends.setViewportView(friendsList);
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((dimension.getWidth() - getWidth()) / 2);
		int y = (int) ((dimension.getHeight() - getHeight()) / 2);
		setLocation(x, y);
		validate();
		repaint();
	}

	public Window_Main getWindow_Main() {
		return this;
	}

	public void setMood(String s) {
		txtrMood.setText(s);
		scrollPaneFriends.repaint();
		scrollPaneFriends.validate();
	}

	public void addFriend(String text) {

		Iterator<OtherPeople> io = otherPeople.iterator();
		Friend f = new Friend(text, "");
		while (io.hasNext()) {
			OtherPeople o = io.next();
			if (o.getName().equals(text)) {
				modelOther.removeElement(o);
				friends.add(f);
				modelFriend.addElement(f);
				io.remove();
			}
		}
		scrollPaneFriends.repaint();
		scrollPaneOtherPeople.repaint();
	}

	public void deleteFriend(String text) {
		Iterator<Friend> i = friends.iterator();
		Friend f = new Friend(text, "");
		OtherPeople o;

		while (i.hasNext()) {

			f = i.next();
			if (f.getName().equals(text)) {
				modelFriend.removeElement(f);
				o = new OtherPeople(f.getName());
				otherPeople.add(o);
				modelOther.addElement(o);
				i.remove();
			}

		}
		scrollPaneFriends.repaint();
		scrollPaneOtherPeople.repaint();
	}

	public boolean checkNotFriend(String text) {
		for (OtherPeople o : otherPeople) {
			if (o.getName().equals(text)) {
				return true;
			}
		}
		return false;
	}

	public boolean checkFriend(String text) {
		for (Friend f : friends) {
			if (f.getName().equals(text)) {
				return true;
			}
		}
		return false;
	}
}
